//
//  AnswerVC.m
//  OldHandPT
//
//  Created by ben on 2018/12/13.
//  Copyright © 2018 Shouny. All rights reserved.
//

#import "AnswerVC.h"
#import "HQDataBaseOperate.h"
#import "COneAnsionModel.h"
#import "UIImageView+WebCache.h"

@interface AnswerVC ()
// url
@property (nonatomic,copy) NSString *url;
// 科目几
@property (nonatomic,copy) NSString *ke;
// 数据源
@property (nonatomic,strong) NSArray *myData;
@property (nonatomic,strong) NSMutableArray *myDataAll;
// 题目
@property (weak, nonatomic) IBOutlet UILabel *questionLabel;
// 图片
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (weak, nonatomic) IBOutlet UIButton *AButton;
@property (weak, nonatomic) IBOutlet UIButton *BButton;
@property (weak, nonatomic) IBOutlet UIButton *CButton;
@property (weak, nonatomic) IBOutlet UIButton *DButton;

@property (weak, nonatomic) IBOutlet UILabel *allLabel;
@property (weak, nonatomic) IBOutlet UILabel *wLabel;

// 答题下标
@property (nonatomic,assign) NSInteger index;

// 回答错误下标
@property (nonatomic,assign) NSInteger wIndex;

//@property (nonatomic,strong) UIButton *oldButton;

@end

@implementation AnswerVC
- (instancetype)initWithUrl:(NSString *)url type:(SKYState)state choose:(ChooseState)chooseState Ke:(NSString *)ke
{
    self = [super init];
    if (self) {
        _url = url;
        _state = state;
        _chooseState = chooseState;
        _ke = ke;
    }
    return self;
}

- (NSMutableArray *)myDataAll
{
    if (_myDataAll == nil) {
        _myDataAll = [[NSMutableArray alloc] init];
    }
    return _myDataAll;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _index = 0;
    _wIndex = 0;
    
    self.title = @"答题";
    [self setUIData];
    [self setUI];
    self.allLabel.text = [NSString stringWithFormat:@"%ld/%ld",_index + 1,self.myData.count];
    self.wLabel.text = [NSString stringWithFormat:@"答错：%ld",_wIndex];
}

- (void)setUI{
    
    COneAnsionModel *model = self.myData[0];
    self.questionLabel.text = model.question?:@"";
    [self.imageView sd_setImageWithURL: [NSURL URLWithString:model.url]];
    NSString *aStr = model.item1?:@"";
    if ([aStr isEqual:@""]) {
        self.AButton.hidden = YES;
    }else{
        self.AButton.hidden = NO;
    }
    [self.AButton setTitle:[NSString stringWithFormat:@"A. %@",aStr] forState:UIControlStateNormal];
    
    NSString *bStr = model.item2?:@"";
    if ([bStr isEqual:@""]) {
        self.BButton.hidden = YES;
    }else{
        self.BButton.hidden = NO;
    }
    [self.BButton setTitle:[NSString stringWithFormat:@"B. %@",bStr] forState:UIControlStateNormal];
    
    NSString *cStr = model.item3?:@"";
    if ([cStr isEqual:@""]) {
        self.CButton.hidden = YES;
    }else{
        self.CButton.hidden = NO;
    }
    [self.CButton setTitle:[NSString stringWithFormat:@"C. %@",cStr] forState:UIControlStateNormal];
    
    NSString *dStr = model.item4?:@"";
    if ([dStr isEqual:@""]) {
        self.DButton.hidden = YES;
    }else{
        self.DButton.hidden = NO;
    }
    [self.DButton setTitle:[NSString stringWithFormat:@"D. %@",dStr] forState:UIControlStateNormal];
}

- (void)setUIData{
    [self.myDataAll removeAllObjects];
    if ([_ke isEqual:@"1"]) {
        if ([_url isEqual:@"data"]) {
            NSDictionary *dic;
            switch (_state) {
                case COne:
                    dic = [self readLocalFileWithName:@"COne"];
                    break;
                case CTwo:
                    dic = [self readLocalFileWithName:@"CTwo"];
                    break;
                case BOne:
                    dic = [self readLocalFileWithName:@"BOne"];
                    break;
                case BTwo:
                    dic = [self readLocalFileWithName:@"BTwo"];
                    break;
                case AOne:
                    dic = [self readLocalFileWithName:@"AOne"];
                    break;
                default:
                    dic = [self readLocalFileWithName:@"ATwo"];
                    break;
            }
            for (NSDictionary *tempDic in dic[@"result"]) {
                COneAnsionModel *model = [COneAnsionModel yy_modelWithJSON:tempDic];
                [self.myDataAll addObject:model];
            }
            switch (_chooseState) {
                case ChooseRandom:
                    [self getSuiJi:self.myDataAll];
                    break;
                    
                default:
                    self.myData = [NSArray arrayWithArray:self.myDataAll];
                    break;
            }
        }else{
            // 错题
            switch (_state) {
                case COne:
                    self.myData = [HQDataBaseOperate queryAllCOneMessage];
                    break;
                case CTwo:
                    self.myData = [HQDataBaseOperate queryAllCTwoMessage];
                    break;
                case BOne:
                    self.myData = [HQDataBaseOperate queryAllBOneMessage];
                    break;
                case BTwo:
                    self.myData = [HQDataBaseOperate queryAllBTwoMessage];
                    break;
                case AOne:
                    self.myData = [HQDataBaseOperate queryAllAOneMessage];
                    break;
                default:
                    self.myData = [HQDataBaseOperate queryAllATwoMessage];
                    break;
            }
        }
    }else{
        NSDictionary *dic = [self readLocalFileWithName:@"KeFour"];
        for (NSDictionary *tempDic in dic[@"result"]) {
            COneAnsionModel *model = [COneAnsionModel yy_modelWithJSON:tempDic];
            [self.myDataAll addObject:model];
        }
        self.myData = [NSArray arrayWithArray:self.myDataAll];
    }
}

// 随机100道题
- (void)getSuiJi:(NSMutableArray *)arr
{
    NSMutableSet *randomSet = [[NSMutableSet alloc] init];
    
    while ([randomSet count] < 100) {
        int r = arc4random() % [arr count];
        [randomSet addObject:[arr objectAtIndex:r]];
    }
    self.myData = [randomSet allObjects];
}

#pragma mark -Click

- (IBAction)chooseAnswer:(UIButton *)sender {
//    _oldButton.selected = NO;
//    sender.selected = YES;
//    _oldButton = sender;
    COneAnsionModel *model = self.myData[_index];
    if ([model.answer integerValue] == sender.tag - 999) {
    }else{
        TKAertCenterManager(@"回答错误")
        _wIndex ++;
        if ([_ke isEqual:@"1"]) {
            switch (_state) {
                case COne:
                    [HQDataBaseOperate queryCOneWithFromUserID:model];
                    break;
                case CTwo:
                    [HQDataBaseOperate queryCTwoWithFromUserID:model];
                    break;
                case BOne:
                    [HQDataBaseOperate queryBOneWithFromUserID:model];
                    break;
                case BTwo:
                    [HQDataBaseOperate queryBTwoWithFromUserID:model];
                    break;
                case AOne:
                    [HQDataBaseOperate queryAOneWithFromUserID:model];
                    break;
                default:
                    [HQDataBaseOperate queryATwoWithFromUserID:model];
                    break;
            }
        }
    }
    self.allLabel.text = [NSString stringWithFormat:@"%ld/%ld",_index + 1,self.myData.count];
    self.wLabel.text = [NSString stringWithFormat:@"答错：%ld",_wIndex];
    _index ++;
    
    if (_index == self.myData.count) {
        [self.navigationController popViewControllerAnimated:YES];
        NSString *tempStr = [NSString stringWithFormat:@"得分%ld分\n错误的题可以去错题库里重新答题。",self.myData.count - _wIndex];
        TKAertCenterManager(tempStr)
        return;
    }
    
    // 重新渲染UI
    COneAnsionModel *aginModel = self.myData[_index];
    self.questionLabel.text = aginModel.question?:@"";
    [self.imageView sd_setImageWithURL: [NSURL URLWithString:aginModel.url]];
    NSString *aStr = aginModel.item1?:@"";
    if ([aStr isEqual:@""]) {
        self.AButton.hidden = YES;
    }else{
        self.AButton.hidden = NO;
    }
    [self.AButton setTitle:[NSString stringWithFormat:@"A. %@",aStr] forState:UIControlStateNormal];
    
    NSString *bStr = aginModel.item2?:@"";
    if ([bStr isEqual:@""]) {
        self.BButton.hidden = YES;
    }else{
        self.BButton.hidden = NO;
    }
    [self.BButton setTitle:[NSString stringWithFormat:@"B. %@",bStr] forState:UIControlStateNormal];
    
    NSString *cStr = aginModel.item3?:@"";
    if ([cStr isEqual:@""]) {
        self.CButton.hidden = YES;
    }else{
        self.CButton.hidden = NO;
    }
    [self.CButton setTitle:[NSString stringWithFormat:@"C. %@",cStr] forState:UIControlStateNormal];
    
    NSString *dStr = aginModel.item4?:@"";
    if ([dStr isEqual:@""]) {
        self.DButton.hidden = YES;
    }else{
        self.DButton.hidden = NO;
    }
    [self.DButton setTitle:[NSString stringWithFormat:@"D. %@",dStr] forState:UIControlStateNormal];
    
}

- (void)setData{
    /*
     subject   选择考试科目类型，1：科目1；4：科目4
     model     驾照类型，可选择参数为：c1,c2,a1,a2,b1,b2；当subject=4时可省略
     testType  测试类型，rand：随机测试（随机100个题目），order：顺序测试（所选科目全部题目）
     */
    self.myData = [HQDataBaseOperate queryAllCOneMessage];
    [LCNetworking getWithURL:_url Params:nil success:^(id responseObject) {
        NSLog(@"GET_success____%@", responseObject);
    } failure:^(NSString *error) {
        NSLog(@"GET_failure____%@", error);
    }];
}

@end
